<!DOCTYPE html>
<html>
<head>
<title>Circular Motion Diagram</title>
</head>
<body>
<canvas id="myCanvas" width="600" height="450" style="border:1px solid #d3d3d3;"></canvas>
<script>
    const canvas = document.getElementById('myCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings
    ctx.lineWidth = 2;
    ctx.font = '20px Arial';
    ctx.fillStyle = 'black';
    ctx.strokeStyle = 'black';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    // Parameters from the problem description and diagram
    const o = { x: 280, y: 120 }; // Pivot point O
    const l = 180; // Length 'l' in pixels
    const ballRadius = 8;
    const theta_deg = 30;
    const theta_rad = theta_deg * Math.PI / 180;

    // --- Draw the main circular path ---
    // The path in the diagram goes from A (0 degrees) to a point past D (120 degrees).
    // Let's draw it up to 135 degrees for a similar visual appearance.
    ctx.beginPath();
    ctx.arc(o.x, o.y, l, 0, 135 * Math.PI / 180);
    ctx.lineWidth = 3;
    ctx.stroke();
    ctx.lineWidth = 2; // Reset line width for other elements

    // --- Define coordinates for key points ---
    const A = { x: o.x + l, y: o.y };
    const B = { x: o.x + l * Math.cos(theta_rad), y: o.y + l * Math.sin(theta_rad) };
    const C = { x: o.x, y: o.y + l };
    // The angle for D is 90 degrees (from horizontal to vertical) + theta
    const d_angle_rad = (Math.PI / 2) + theta_rad; // Angle of OD from horizontal
    const D = { x: o.x + l * Math.cos(d_angle_rad), y: o.y + l * Math.sin(d_angle_rad) };

    // --- Draw dashed lines (OB, OC, OD) ---
    ctx.beginPath();
    ctx.setLineDash([5, 5]);
    // Line OB
    ctx.moveTo(o.x, o.y);
    ctx.lineTo(B.x, B.y);
    // Line OC
    ctx.moveTo(o.x, o.y);
    ctx.lineTo(C.x, C.y);
    // Line OD
    ctx.moveTo(o.x, o.y);
    ctx.lineTo(D.x, D.y);
    ctx.stroke();
    ctx.setLineDash([]); // Reset to solid line style

    // --- Draw solid line OA ---
    ctx.beginPath();
    ctx.moveTo(o.x, o.y);
    ctx.lineTo(A.x, A.y);
    ctx.stroke();

    // --- Draw the ball at position A ---
    ctx.beginPath();
    ctx.arc(A.x, A.y, ballRadius, 0, 2 * Math.PI);
    ctx.fill();

    // --- Draw angle arcs ---
    ctx.lineWidth = 1;
    // Angle theta between OA and OB
    const angle_arc_radius = 40;
    ctx.beginPath();
    ctx.arc(o.x, o.y, angle_arc_radius, 0, theta_rad);
    ctx.stroke();
    // Angle theta between OC and OD
    ctx.beginPath();
    ctx.arc(o.x, o.y, angle_arc_radius, Math.PI / 2, d_angle_rad);
    ctx.stroke();
    ctx.lineWidth = 2; // Reset line width

    // --- Add labels ---
    // Point labels
    ctx.fillText('O', o.x - 15, o.y - 15);
    ctx.fillText('A', A.x + 20, A.y);
    ctx.fillText('B', B.x + 20, B.y + 10);
    ctx.fillText('C', C.x, C.y + 20);
    ctx.fillText('D', D.x - 20, D.y);

    // Other labels (m, l)
    ctx.fillText('l', o.x + l / 2, o.y - 15);
    ctx.fillText('m', A.x + 15, A.y - 20);

    // Angle labels (θ)
    const angle_label_radius = angle_arc_radius + 10;
    ctx.fillText('θ', o.x + angle_label_radius * Math.cos(theta_rad / 2), o.y + angle_label_radius * Math.sin(theta_rad / 2));
    const angle_cod_mid = (Math.PI / 2 + d_angle_rad) / 2;
    ctx.fillText('θ', o.x + angle_label_radius * Math.cos(angle_cod_mid), o.y + angle_label_radius * Math.sin(angle_cod_mid));

    // Caption
    ctx.font = '22px KaiTi'; // Use a font that supports Chinese characters
    ctx.fillText('习题 2-18 图', canvas.width / 2, canvas.height - 30);
</script>
</body>
</html>